//工具类组件 index.js
import { readFile } from 'fs/promises';
import * as fs from 'fs'

import TestJson from "@/data/equip.json";
import levelJson from "@/data/level.json";
import MonsterJson from "@/data/monster.json";

import MaterialJson from "@/data/material.json";
import LevelJson from "@/data/level.json";
import SkillsJson from "@/data/Skills.json";
export const getAssetURL = (image: any) => {
    // 参数:相对路径,当前文件路径URL
    return new URL(`../assets/images/${image}`, import.meta.url).href
}
export const equipFindOne = (value: String) => {
let  data=TestJson

return data[value]
    // 参数:相对路径,当前文件路径URL
    // return new URL(`../assets/images/${image}`,import.meta.urfl).href
}
export const getMonsterOne = (value: String) => {
    let  data=MonsterJson
    let selectedData;  
    for (const monster in data) {  
        if (data[monster].map === value) {  
            selectedData = data[monster];  
            break; // 找到后退出循环  
        }  
    }  
    return selectedData
        // 参数:相对路径,当前文件路径URL
        // return new URL(`../assets/images/${image}`,import.meta.urfl).href
    }

    export const getMaterialOne = (value: String) => {
        let  data=MaterialJson
        let selectedData;  
        for (const monster in data) {  
            console.log(monster)
            if (monster === value) {  
                selectedData = data[monster];  
                break; // 找到后退出循环  
            }  
        } 
        return selectedData
        }
        export const getLevel = (value: Number) => {
            let  data=LevelJson
            let selectedData;  
            for (const monster in data) {  
                if (Number(monster) == value) {  
                    selectedData = data[monster].name;  
                    break; // 找到后退出循环  
                }  
            } 
            return selectedData
            }
        export const getsSkillsOne = (value: any) => {
            let  data=SkillsJson
            let selectedData;  
            for (const monster in data) {  

                if (data[monster].id === value) {  
                    selectedData = data[monster];  
                    break; // 找到后退出循环  
                }  
            }  

     
            return selectedData
                // 参数:相对路径,当前文件路径URL
                // return new URL(`../assets/images/${image}`,import.meta.urfl).href
            }






export    function parseCondition(condition:any) {
    const conditions = <any>[];
    const length = condition.length;
    const stack = <any>[];
    stack.unshift(conditions);
    let cursor = 0;
    const catchString = (i: number) => {
        const str = condition.substring(cursor, i).trim();
        cursor = i;
        if(str) stack[0].push(str);
    };
    for(let i=0; i<length; i++) {
        switch(condition[i]) {
            case ' ': continue;
            case '(':
                catchString(i);
                cursor ++;
                const sub: never[] = [];
                stack[0].push(sub);
                stack.unshift(sub);
                break;

            case ')':
                catchString(i);
                cursor ++;
                stack.shift();
                break;

            case '|':
            case '&':
                catchString(i);
                catchString(i+1);
                break;
            default: continue;
        }
    }

    catchString(length);

    return conditions;
}

export function checkCondition(property: any, condition: any) {  
    const conditions = parseCondition(condition);  
    return checkParsedConditions(property, conditions);  
}  
  
function checkParsedConditions(property: any, conditions: string | any[]): boolean {  
    if(!Array.isArray(conditions)) return checkProp(property, conditions);  
    if(conditions.length == 0) return true;  
    if(conditions.length == 1) return checkParsedConditions(property, conditions[0]);  
  
    let ret = checkParsedConditions(property, conditions[0]);  
    for(let i=1; i<conditions.length; i+=2) {  
        switch(conditions[i]) {  
            case '&':  
                if(ret) ret = checkParsedConditions(property, conditions[i+1]);  
                break;  
            case '|':  
                if(ret) return true;  
                ret = checkParsedConditions(property, conditions[i+1]);  
                break;  
            default: return false;  
        }  
    }  
    return ret;  
}

function checkProp(property: { get: (arg0: any) => any; }, condition: string) {
    const length = condition.length;
    let i = condition.search(/[><\!\?=]/);
    const prop = condition.substring(0,i);
    const symbol = condition.substring(i, i+=(condition[i+1]=='='?2:1));
    const d = condition.substring(i, length);
    const conditionData = d[0]=='['? JSON.parse(d): Number(d);
    console.log(property,"prop",conditionData)
    const propData = property;
    switch(symbol) {
        case '>':  return propData >  conditionData;
        case '<':  return propData <  conditionData;
        case '>=': return propData >= conditionData;
        case '<=': return propData <= conditionData;
        case '=':
            if(Array.isArray(propData))
                return propData.includes(conditionData);
            return propData == conditionData;
        case '!=':
            if(Array.isArray(propData))
                return !propData.includes(conditionData);
            return propData == conditionData;
        case '?':
            if(Array.isArray(propData)) {
                for(const p of propData)
                    if(conditionData.includes(p)) return true;
                return false;
            }
            return conditionData.includes(propData);
        case '!':
            if(Array.isArray(propData)) {
                for(const p of propData)
                    if(conditionData.includes(p)) return false;
                return true;
            }
            return !conditionData.includes(propData);

        default: return false;
    }
}
export function getRealm(value: string) {  
    let data: DataItem[] = Object.values(levelJson); // 明确指定 data 的类型为 DataItem[]  
    interface DataItem {    
        id: string;    
        name: string;    
    }  
    let filteredData = data.filter((item: DataItem) => item.id == value); // 过滤后的元素类型仍然是 DataItem[]  
    if(filteredData.length>0){
 let name = (filteredData[0] as DataItem).name;
 return name;  
    }
}




















// function extractMaxTriggers(condition) {
//     // Assuming only age related talents can be triggered multiple times.
//     const RE_AGE_CONDITION = /AGE\?\[([0-9\,]+)\]/;
//     const match_object = RE_AGE_CONDITION.exec(condition);
//     if (match_object == null) {
//         // Not age related, single trigger.
//         return 1;
//     }
    
//     const age_list = match_object[1].split(",");
//     return age_list.length;
// }
// export { checkCondition, extractMaxTriggers };